
package backupUtility;
import java.awt.HeadlessException;
import java.io.File;
import java.util.ArrayList;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
/**
 * This class represents the main interface of the program
 * it also contains the main method for the whole project
 * @author Changcoco Kevin Paolo
 * @author Mamis Dominique Bernard
 * @author Palasi Jandy
 * @author Rafael Ian Paolo 
 */
public class BackupUtilityUI extends javax.swing.JFrame {
/**
* @extends javax.swing.JFrame		Holds all the JComponents/Main Interface
*/
    String backupFolder = "";
    String pword = "";
    ArrayList<File> selectedFiles;
    
    public BackupUtilityUI() {
        initComponents();
        jButton1.setEnabled(true);
    }
    
    public BackupUtilityUI(ArrayList<File> sFiles, String bufolder,String pass) {
        initComponents();
        addToBackupButton.setEnabled(true);
        jButton1.setEnabled(true);
        this.backupFolder = bufolder;
        this.selectedFiles = sFiles;
        this.pword = pass;
    }
    
  
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        deleteBackupButton = new javax.swing.JButton();
        addToBackupButton = new javax.swing.JButton();
        changePasswordButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("Create Backup");
        jButton1.setEnabled(true);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        deleteBackupButton.setText("Delete Backup");
        deleteBackupButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteBackupButtonActionPerformed(evt);
            }
        });

        addToBackupButton.setText("Add to Backup");
        addToBackupButton.setEnabled(true);
        addToBackupButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addToBackupButtonActionPerformed(evt);
            }
        });

        changePasswordButton.setText("Change Backup Password");
        changePasswordButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                changePasswordButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(48, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(deleteBackupButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(42, 42, 42)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(changePasswordButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(addToBackupButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(38, 38, 38))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addToBackupButton, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(changePasswordButton, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(deleteBackupButton, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(35, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
	/**
	* Create new FileListUI
	* @param java.qwt.event.ActionEvent evt
	*/
    

    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        FileListUI flg = new FileListUI();
        dispose();
    }//GEN-LAST:event_jButton1ActionPerformed
	/**
	* Zipping or Archiving the backup folder
	*/
    private void changePasswordButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_changePasswordButtonActionPerformed
      JOptionPane.showMessageDialog(null,"Choose zip Backup.");
      final JFileChooser fileChooser = new JFileChooser(new File("C:/BACKUP/"));
      backupFolder = "C:/BACKUP/";
      fileChooser.setFileFilter(new FileFilter(){ 
		/**
		* return zipped backup folder
		*
		* @param File f
		*/
       public boolean accept(File f) {
            if (f.isDirectory()) {
                return true;
            }
            final String name = f.getName();
            return name.endsWith(".zip");
        }
		/**
		* returns specific zipped backup folder
		*/
        public String getDescription() {
            return "*.zip";
        }
    });
    fileChooser.showOpenDialog(this);
      
    }//GEN-LAST:event_changePasswordButtonActionPerformed

    private void addToBackupButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addToBackupButtonActionPerformed
      JOptionPane.showMessageDialog(null,"Choose BACKUP ZIP File."); 
      final JFileChooser fileChooser = new JFileChooser(new File("C:/BACKUP/"));
      backupFolder = "C:/BACKUP/";
      fileChooser.setFileFilter(new FileFilter(){ 

       public boolean accept(File f) {
            if (f.isDirectory()) {
                return true;
            }
            final String name = f.getName();
            return name.endsWith(".zip");
        }
        public String getDescription() {
            return "*.zip";
        }
    });
    fileChooser.showOpenDialog(this);
    try {
	File chosen = fileChooser.getSelectedFile();
        
	    String pass = JOptionPane.showInputDialog("Enter your Password: ");
            
            ZipFile zip;
            File location = fileChooser.getSelectedFile().getParentFile();
            
               zip = new ZipFile(chosen);
               //if(zip.isEncrypted()){
                     zip.setPassword(pass);
                     String path = location.getPath();
                     zip.extractAll(path);
                     //chosen.delete();
                     File [] extracted = location.listFiles();
                     ArrayList<File> newZip = new ArrayList<File>();
                        for(File f : extracted){
                            AddBackupHere abh = new AddBackupHere(selectedFiles, backupFolder, pword);
                            dispose();
                     }
                     JOptionPane.showMessageDialog(null,"Password Validation Ok.");
               //}

           } catch (ZipException | HeadlessException ex) {
                     JOptionPane.showMessageDialog(null, "Wrong Password!!!");
           }
	                       
       //AddBackupHere abh = new AddBackupHere(selectedFiles, backupFolder, pword);
       //   dispose();       
    }//GEN-LAST:event_addToBackupButtonActionPerformed

    private void deleteBackupButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteBackupButtonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_deleteBackupButtonActionPerformed
	
	/**
	* Main argument
	*/
    public static void main(String args[]) {

        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(BackupUtilityUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(BackupUtilityUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(BackupUtilityUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(BackupUtilityUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
       
       //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(BackupUtilityUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(BackupUtilityUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(BackupUtilityUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(BackupUtilityUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        try {
            for(int s=3;s>=0;s--){
      		Thread.sleep(1000);
      		if (s == 0) {
                    int n = JOptionPane.showConfirmDialog(null,"It's Almost time, Would you like to back up your files?","",JOptionPane.YES_NO_OPTION);		
                        if(n == JOptionPane.YES_OPTION){
                            java.awt.EventQueue.invokeLater(new Runnable() {
                                public void run() {
                                new BackupUtilityUI().setVisible(true);
                                }});
                        }
                        else{
                            System.exit(0);
                        }
                 }

   		}
        } catch(Exception exc) {
        	System.out.println(exc);

        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addToBackupButton;
    private javax.swing.JButton changePasswordButton;
    private javax.swing.JButton deleteBackupButton;
    private javax.swing.JButton jButton1;
    // End of variables declaration//GEN-END:variables
}
